package com.example.demo.rest.lenovoid;

import com.example.demo.api.lenovoid.LenovoIDApi;
import com.example.demo.api.lenovoid.LenovoIDApi2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


@RestController
@Slf4j
public class LenovoIdController {
    @Autowired
    LenovoIDApi lenovoIDApi;
    @Autowired
    LenovoIDApi2 lenovoIDApi2;
    public static final String realm = "account.lenovo.com";

    @GetMapping("lenovo/{username}/{password}")
    public Object getustbypwd(@PathVariable("username") String username, @PathVariable("password") String password) {
        return lenovoIDApi.getustbypwd(username, password, realm, "uuid", "lenovoidcookie", realm, "en_US", "1");
    }

    @GetMapping("lenovo/token/account/{token}")
    public Object getaccount(@PathVariable("token") String token) {
        return lenovoIDApi.getaccountid(realm, token);
    }

    @GetMapping("lenovo/token/user/{token}")
    public Object getuserInfo(@PathVariable("token") String token) {
        return lenovoIDApi2.userkeyinfo(realm, token);
    }


    public static String REGEX_TOKEN = "[a-zA-Z0-9-]{193}";// 匹配Token
    public static String REGEX_HTTP = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]"; //url

    public static String find(String regex, String text) {
        StringBuilder sb = new StringBuilder();
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(text);
        if (m.find()) {
            sb.append(m.group());
        }
        return sb.toString();
    }


}
